package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.service.IRaceInfoService;
import com.zerdoor.util.exception.FeeCardNotEnoughException;
import com.zerdoor.util.exception.InsufficientBalanceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/11/03.
 * 项目任务-发布悬赏需求 控制层
 */
@Controller
@RequestMapping("/raceInfo")
public class RaceInfoController {

    @Autowired
    private IRaceInfoService raceInfoService;


    /**
     * 跳转到项目任务-发布需求界面
     * @return
     */
    @RequestMapping(value = "/raceInfo", method = RequestMethod.GET)
    public String taskInfo(HttpServletRequest request) {

        //获取当前用户ID
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

        try {
            Map<String,Object> map = raceInfoService.findInitData(userInfo.getUserId());

            request.setAttribute("initData",map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "/raceInfo/raceInfo";
    }


    /**
     * 跳转到支付成功的页面
     * @return
     */
    @RequestMapping(value = "/paySuccess", method = RequestMethod.GET)
    public String paySuccess(String itemName, HttpServletRequest request) {

        request.setAttribute("itemName",itemName);
        return "/raceInfo/paySuccess";
    }

    /**
     * Ajax根据技能ID集查询需求技能
     * @return
     */
    @RequestMapping(value = "/findSkillBySkillIds", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> findSkillBySkillIds(String skillIds) {



        try {

            List<Map<String,String>> map = null;
            if(!"".equals(skillIds)){

                map = raceInfoService.findSkillBySkillIds(skillIds);
            }


            ResponseEntity<List<Map<String,String>>> responseEntity = new ResponseEntity<List<Map<String,String>>>(map, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }






    /**
     * 新增需求信息
     * @param param 需求数据
     * @return
     */
    @RequestMapping(value = "/insertTaskInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> insertTaskInfo(@RequestParam Map param,HttpServletRequest request) {



        try {

            //获取当前用户ID
            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
            param.put("user_id",userInfo.getUserId().toString());

            String language = "zh_cn";
            //从session中取出当前语言环境
            if(request.getSession().getAttribute("language") != null){

                language = request.getSession().getAttribute("language").toString();
            }

            param.put("lang",language);

            if(param.get("race_id") == null || "".equals(param.get("race_id"))){

                //新增任务
                raceInfoService.addTaskInfoAndAddvalue(param);
            }else{

                //验证当前进度是否还可以被修改
                boolean flag = raceInfoService.isRaceInfoProgress(String.valueOf(param.get("race_id")));

                //false 为不可被修改
                if(!flag){

                    ResponseEntity<String> responseEntity = new ResponseEntity<String>("debug", HttpStatus.OK);
                    return responseEntity;
                }

                raceInfoService.updateItemInfoByItemId(param);
            }



            String race_id = String.valueOf(param.get("race_id"));

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(race_id, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("error", HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }




    /**
     * 确认需求页面
     * @return
     */
    @RequestMapping(value = "/confirmRaceInfo", method = RequestMethod.GET)
    public String confirmRaceInfo(String raceId,HttpServletRequest request) {



        try {
            Map<String,Object> map =  raceInfoService.updateFindTaskInfoByTaskId(raceId);

            //String json = JSONUtils.toJSONString(map);

            request.setAttribute("raceInfoMap",map);
            //request.setAttribute("taskId",taskId);

            return "/raceInfo/confirmRaceInfo";
            //ResponseEntity<Map<String,Object>> responseEntity = new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
            //return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return "/raceInfo/confirmRaceInfo";
        }




    }



    /**
     * 跳转到发布并支付页面
     * @return
     */
    @RequestMapping(value = "/publishAndPay", method = RequestMethod.GET)
    public String publishAndPay(String raceId,HttpServletRequest request) {



        try {

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");


            Map<String,Object> payInfo =  raceInfoService.updateFindAddvalueByTaskId(raceId,userInfo.getUserId().toString());

            //String json = JSONUtils.toJSONString(list);

            request.setAttribute("payInfo",payInfo);
            request.setAttribute("raceId",raceId);

            return "/raceInfo/publishAndPay";

        } catch (Exception e) {

            e.printStackTrace();
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return "/raceInfo/publishAndPay";
        }




    }





    /**
     * 支付宝支付增值服务费用
     * @param task_id 任务信息ID
     * @return
     */
    @RequestMapping(value = "/addvaluePayByAlipay", method = RequestMethod.POST)
    public ResponseEntity<?> addvaluePayByAlipay(String task_id,HttpServletRequest request) {



        try {


            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            String userID = userInfo.getUserId().toString();
            boolean flag = raceInfoService.addvaluePayByAlipay(task_id,userID);



            ResponseEntity<Boolean> responseEntity = new ResponseEntity<Boolean>(flag, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }


    /**
     * paypay支付增值服务费用
     * @param task_id 任务信息ID
     * @return
     */
    @RequestMapping(value = "/addvaluePayByPaypay", method = RequestMethod.POST)
    public ResponseEntity<?> addvaluePayByPaypay(String task_id,HttpServletRequest request) {



        try {




            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            String userID = userInfo.getUserId().toString();
            boolean flag = raceInfoService.addvaluePayByPaypay(task_id,userID);



            ResponseEntity<Boolean> responseEntity = new ResponseEntity<Boolean>(flag, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }

    /**
     * 余额支付增值服务费用
     * @param race_id 竞赛活动ID
     * @return
     */
    @RequestMapping(value = "/addvaluePayByBalance", method = RequestMethod.POST)
    public ResponseEntity<?> addvaluePayByBalance(String race_id,HttpServletRequest request) {

        Map<String, Object> data = new HashMap<String, Object>();
        try {

            UserInfo userInfo = (UserInfo) request.getSession().getAttribute("curUser");

            String userID = userInfo.getUserId().toString();

            data = raceInfoService.addvaluePayByBalance(race_id, userID);



        }catch (InsufficientBalanceException e){

            data.put("static","1");
            data.put("msg","支付失败！余额不足或账户被冻结！");

        }catch (FeeCardNotEnoughException e){

            data.put("static","2");
            data.put("msg","零手续费卡不足！无法使用零手续费卡");

        }catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

        ResponseEntity<Map<String, Object>> responseEntity = new ResponseEntity<Map<String, Object>>(data, HttpStatus.OK);
        return responseEntity;


    }







}
